package com.itheima.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.AjaxMsg;
import com.itheima.domain.Employee;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 利用ajax请求方式和rest风格
 */
@Controller
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 通过批量id删除用户, 利用POST请求
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteAll", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg deleteEmp(Integer[] ids){
        employeeService.deleteEmpsByIds(ids);
        return AjaxMsg.success();
    }


    /**
     * 通过id删除员工信息 通过DELETE请求
     * @param eid
     * @return
     */
    @RequestMapping(value = "/{empId}", method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxMsg deleteEmp(@PathVariable("empId")Integer eid){
        employeeService.deleteById(eid);
        return AjaxMsg.success();
    }


    /**
     * 通过id修改用户信息 通过PUT请求
     * @param employee
     * @return
     */
    @RequestMapping(value = "/{empId}", method = RequestMethod.PUT)
    @ResponseBody
    public AjaxMsg updateEmp(Employee employee){
        System.out.println("方法执行");
        employeeService.updateEmpById(employee);
        return AjaxMsg.success();
    }

    /**
     * 根据id查询一个用户, 利用GET请求才行
     * @param eid
     * @return
     */
    @RequestMapping(value="/{eid}", method = RequestMethod.GET)
    @ResponseBody
    public AjaxMsg findOneByIdWithEandD(@PathVariable("eid")Integer eid){
        Employee empAndDept = employeeService.findOneByIdWithEandD(eid);
        return AjaxMsg.success().add("employee", empAndDept);
    }


    /**
     * 将获取到的数据插入到数据库中, 通过POST请求才能使用该方法
     * url -> localhost/emp post
     * @param emp
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg insert(Employee emp){
        employeeService.insert(emp);
        return AjaxMsg.success();
    }

    /**
     * 查询分页信息, 存放到域中
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value="pageNum", defaultValue = "1")Integer pageNum, Model model){
        /*利用page分页插件来对查询的列表进行分页*/
        PageHelper.startPage(pageNum, 5);
        /*查询数据*/
        List<Employee> emps = employeeService.findAll();
        /*将查询到的数据进行封装*/
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(emps, 5);
        /*将数据存放到域中*/
        model.addAttribute("pageInfo", pageInfo);
        return "list";
    }

    /**
     * 查询分页信息, 将信息封装, 利用JSON数据发送给客户端
     * @param pageNum
     * @return
     */
    @RequestMapping("/findPageInfo")
    @ResponseBody
    public AjaxMsg findPageInfo(@RequestParam(value="pageNum", defaultValue = "1")Integer pageNum){
        /*利用page分页插件来对查询的列表进行分页*/
        PageHelper.startPage(pageNum, 5);
        /*查询数据*/
        List<Employee> emps = employeeService.findAll();
        /*将查询到的数据进行封装*/
        PageInfo<Employee> pageInfo = new PageInfo<>(emps, 5);
        /*通过静态方法创建一个对象, 并将pageInfo封装到数据域中*/
        return AjaxMsg.success().add("pageInfo", pageInfo);
    }

}
